package http.network;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author zhuhuogeng
 * @date 2017/5/6.
 */
public class HttpCallBackPool {
  private static HttpCallBackPool instance = new HttpCallBackPool();
  private static Logger log = LoggerFactory.getLogger(HttpCallBackPool.class);

  private Queue<HttpCallback> inactive = new LinkedList<>();

  private HttpCallBackPool() {
  }

  public static HttpCallBackPool getInstance() {
    return instance;
  }

  /**
   * 获取一个HttpCallBack
   *
   * @return
   */
  public HttpCallback allocate(long itemId, int successEventType, int failureEventType) {
    HttpCallback httpCallback = inactive.poll();
    if (httpCallback == null) {
      httpCallback = new HttpCallback();
    }
    httpCallback.setItemId(itemId);
    httpCallback.setSuccessEventType(successEventType);
    httpCallback.setFailureEventType(failureEventType);
    return httpCallback;
  }

  /**
   * 释放HttpCallBack
   *
   * @param
   */
  public void release(HttpCallback httpCallback) {
//    httpCallback.setItemId(EventBus.INVALID_ITEM_ID);
    inactive.add(httpCallback);
    log.info("httpCallbackPool size: {}", inactive.size());
  }

}
